//
//  YKAppDelegate.m
//  yuke
//
//  Created by Dejun Liu on 15/1/5.
//  Copyright (c) 2015年 juwan. All rights reserved.
//

#import "YKAppDelegate.h"
#import "YKHomeViewController.h"
#import "YKDestinationViewController.h"
#import "YKCategoryViewController.h"
#import "YKUserViewController.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import "YKLoginViewController.h"

@implementation YKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self setupViewControllers];
    [self.window makeKeyAndVisible];
    [self customizeInterface];
    
    self.window.rootViewController = self.viewController;
    bool isTest = NO;
    if (isTest) {
        self.window.rootViewController = [[YKLoginViewController alloc]init];
    }
    
    return YES;
}

#pragma mark - Methods

- (void)setupViewControllers {
    UIViewController *firstViewController = [[YKHomeViewController alloc] init];
    UIViewController *firstNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:firstViewController];
    
    UIViewController *secondViewController = [[YKDestinationViewController alloc] init];
    UIViewController *secondNavigationController = [[UINavigationController alloc]
                                                    initWithRootViewController:secondViewController];
    
    UIViewController *thirdViewController = [[YKCategoryViewController alloc] init];
    UIViewController *thirdNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:thirdViewController];
    
    UIViewController *forthViewController = [[YKUserViewController alloc] init];
    UIViewController *forthNavigationController = [[UINavigationController alloc]
                                                   initWithRootViewController:forthViewController];
    
    RDVTabBarController *tabBarController = [[RDVTabBarController alloc] init];
    [tabBarController setViewControllers:@[firstNavigationController, secondNavigationController,
                                           thirdNavigationController,forthNavigationController]];
    self.viewController = tabBarController;
    
    [self customizeTabBarForController:tabBarController];
}

- (void)customizeTabBarForController:(RDVTabBarController *)tabBarController {
    UIImage *tarbarSelectedImage = [UIImage imageNamed:@"tabBarSelectBg"];
    NSArray *tabBarItemImages = @[@"home", @"destination", @"category",@"user"];
    NSArray *tarBarItemTitles = @[@"首页",@"目的地",@"分类",@"我的"];
    NSDictionary *tabBarItemSelectAttributes = @{
                         NSFontAttributeName: [UIFont systemFontOfSize:12],
                         NSForegroundColorAttributeName: UIColorFromRGB(56, 171, 184),
                         };
    NSDictionary *tabBarItemUnSelectAttributes = @{
                                                 NSFontAttributeName: [UIFont systemFontOfSize:12],
                                                 NSForegroundColorAttributeName: UIColorFromRGB(99, 101, 100),
                                                 };
    UIOffset offset = UIOffsetMake(0, 1);
    
    NSInteger index = 0;
    for (RDVTabBarItem *item in [[tabBarController tabBar] items]) {
        UIImage *selectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_on",
                                                      [tabBarItemImages objectAtIndex:index]]];
        UIImage *unselectedimage = [UIImage imageNamed:[NSString stringWithFormat:@"%@_off",
                                                        [tabBarItemImages objectAtIndex:index]]];
        [item setBackgroundColor:UIColorFromRGB(242, 242, 242)];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        item.title = [tarBarItemTitles objectAtIndex:index];
        item.selectedTitleAttributes = tabBarItemSelectAttributes;
        item.unselectedTitleAttributes = tabBarItemUnSelectAttributes;
        item.titlePositionAdjustment = offset;
        [item setBackgroundSelectedImage:tarbarSelectedImage withUnselectedImage:nil];
        index++;
    }
}

- (void)customizeInterface {
    UINavigationBar *navigationBarAppearance = [UINavigationBar appearance];
    
    UIImage *backgroundImage = nil;
    NSDictionary *textAttributes = nil;
    
    if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1) {
        backgroundImage = [UIImage imageNamed:@"navigationbar_background_tall"];
        
        textAttributes = @{
                           NSFontAttributeName: [UIFont boldSystemFontOfSize:18],
                           NSForegroundColorAttributeName: [UIColor blackColor],
                           };
    } else {
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_7_0
        backgroundImage = [UIImage imageNamed:@"navigationbar_background"];
        
        textAttributes = @{
                           UITextAttributeFont: [UIFont boldSystemFontOfSize:18],
                           UITextAttributeTextColor: [UIColor blackColor],
                           UITextAttributeTextShadowColor: [UIColor clearColor],
                           UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetZero],
                           };
#endif
    }
    
    [navigationBarAppearance setBackgroundImage:backgroundImage
                                  forBarMetrics:UIBarMetricsDefault];
    [navigationBarAppearance setTitleTextAttributes:textAttributes];
}


@end
